package com.ruoyi.system.V2.tool.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.V2.font.mapper.QuotationMapper;
import com.ruoyi.system.V2.tool.mapper.BacklistMapper;
import com.ruoyi.system.V2.tool.service.IBacklistService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.model.domain.Backlist;
import com.ruoyi.system.model.domain.Quotation;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-28
 */
@Service
public class BacklistServiceImpl implements IBacklistService {
    @Autowired
    private BacklistMapper backlistMapper;
    @Autowired
    private QuotationMapper quotationMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Backlist selectBacklistById(Long id) {
        Backlist backlist = backlistMapper.selectBacklistById(id);
        if (Objects.nonNull(backlist)) {
            backlist.setTel(null);
            backlist.setUserPhone(null);
        }
        return backlist;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param backlist 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Backlist> selectBacklistList(Backlist backlist) {
        backlist.setStatus(0l);
        Quotation quotation = new Quotation();
        quotation.setCreateId(SecurityUtils.getUserId());
        List<Quotation> quotations = quotationMapper.selectQuotationList(quotation);
        if (CollectionUtils.isNotEmpty(quotations)) {
            List<Long> collect = quotations.stream().map(q -> q.getBackId()).collect(Collectors.toList());
            backlist.setExtIds(collect);
        }
        List<Backlist> backlists = backlistMapper.selectBacklistList(backlist);
        for (Backlist b : backlists) {
            b.setAccount(null);
            b.setTel(null);
            b.setUserPhone(null); //隐藏手机号
        }
        return backlists;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param backlist 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBacklist(Backlist backlist) {
        backlist.setCreateTime(DateUtils.getNowDate());
        backlist.setCreateId(SecurityUtils.getUserId());
        backlist.setStatus(0l);
        return backlistMapper.insertBacklist(backlist);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param backlist 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBacklist(Backlist backlist) {
        backlist.setUpdateTime(DateUtils.getNowDate());
        backlist.setUpdateId(SecurityUtils.getUserId());
        return backlistMapper.updateBacklist(backlist);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBacklistByIds(Long[] ids) {
        return backlistMapper.deleteBacklistByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBacklistById(Long id) {
        return backlistMapper.deleteBacklistById(id);
    }

    @Override
    public List<Backlist> allList(Backlist backlist) {
        backlist.setCreateId(SecurityUtils.getUserId());
        List<Backlist> backlistList = backlistMapper.selectBacklistList(backlist);
        for (Backlist b : backlistList) {
            Quotation quotation = new Quotation();
            quotation.setBackId(b.getId());
            List<Quotation> quotations = quotationMapper.selectQuotationList(quotation);
            b.setBcountt(quotations.size());
            if (CollectionUtils.isNotEmpty(quotations)) {
                Collections.sort(quotations,
                        (o1, o2) -> Integer.parseInt(o2.getBprice().toString()) - Integer.parseInt(o1.getBprice().toString()));
                b.setMaxPrice(quotations.get(0).getBprice());
            }
        }
        return backlistList;
    }

    @Override
    @Transactional
    public int insertH5Backlist(Backlist backlist) {
        backlist.setCreateTime(DateUtils.getNowDate());
        SysUser user = userMapper.checkPhoneUnique(backlist.getUserPhone());
        if (Objects.isNull(user)) {
            user = new SysUser();
            user.setUserName(backlist.getTel());
            user.setNickName(backlist.getTel());
            user.setPhonenumber(backlist.getTel());
            user.setPassword(SecurityUtils.encryptPassword("123456"));
            user.setUserNewType(2);
            user.setStatus("0");
            userService.insertUser(user);
        }
        backlist.setCreateId(user.getUserId());
        backlist.setStatus(0l);
        return backlistMapper.insertBacklist(backlist);
    }

    @Override
    public List<Backlist> selectListH5ForTel(String tel) {
        SysUser user = userMapper.checkPhoneUnique(tel);
        Long createId = -1l;
        if (Objects.nonNull(user)) {
            createId = user.getUserId();
        }
        Backlist backlist = new Backlist();
        backlist.setCreateId(createId);
        List<Backlist> backlistList = backlistMapper.selectBacklistList(backlist);
        for (Backlist b : backlistList) {
            Quotation quotation = new Quotation();
            quotation.setBackId(b.getId());
            List<Quotation> quotations = quotationMapper.selectQuotationList(quotation);
            b.setBcountt(quotations.size());
            if (CollectionUtils.isNotEmpty(quotations)) {
                Collections.sort(quotations,
                        (o1, o2) -> Integer.parseInt(o2.getBprice().toString()) - Integer.parseInt(o1.getBprice().toString()));
                b.setMaxPrice(quotations.get(0).getBprice());
            }
        }
        return backlistList;
    }
}
